package com.lh.pospi.x_store.ui;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import com.lh.pospi.x_store.R;
import com.lh.pospi.x_store.adapter.DeleteAdapter;
import com.lh.pospi.x_store.base.BaseActivity;
import com.lh.pospi.x_store.modle.ConditionMsg;
import com.lh.pospi.x_store.modle.TypeBean;
import com.lh.pospi.x_store.utils.BillUtil;
import com.lh.pospi.x_store.utils.Constant;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.vondear.rxtool.view.RxToast;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class DeleteActivity extends BaseActivity {
    @BindView(R.id.rv_view)
    RecyclerView recyclerView;
    @BindView(R.id.spinner)
    Spinner spinner;
    @BindView(R.id.btn_select)
    QMUIRoundButton btn_select;
    private String[] statuses = new String[]{"全部","已上传", "未上传"};
    private String sta = "全部";
    private List<TypeBean> beans;
    private DeleteAdapter adapter;

    @Override
    protected int getContentView() {
        return R.layout.delete_activity;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        getTopBar().setTitle("订单管理");
        Button upButton = getTopBar().addRightTextButton("删除", R.mipmap.icon_up);
        upButton.setTextColor(getResources().getColor(R.color.white));
        upButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                delete();
            }
        });
        initSpinner();
        initRecycler();
        btn_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (TypeBean bean : beans) {
                    bean.setCheck(true);
                }
                adapter.notifyDataSetChanged();

            }
        });



    }

    private void delete() {
        int i = 0;
        for (TypeBean bean : beans) {
            if (!bean.isCheck()) {
                i++;
            }
        }
        if (i == beans.size()) {
            RxToast.warning("请选择项目");
            return;
        }

        new AlertDialog.Builder(mContext).setTitle("提示")
                .setMessage("是否删除所选单据？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        for (TypeBean bean : beans) {
                            if (bean.isCheck()) {
                                BillUtil.deleteBill(bean, sta);
                            }
                        }
                        RxToast.normal("删除成功");
                    }
                })
                .setNegativeButton("取消", null).show();
    }

    private void initRecycler() {
        beans = new ArrayList<>();
        beans.add(new TypeBean("采购订单",false,Constant.TYPE_D1));
        beans.add(new TypeBean("采购入库",false,Constant.TYPE_RK));
        beans.add(new TypeBean("采购退厂",false,Constant.TYPE_TC));
        beans.add(new TypeBean("批发销售",false,Constant.TYPE_TC));
        beans.add(new TypeBean("批发退货",false,Constant.TYPE_PF));
        beans.add(new TypeBean("盘点单",false,Constant.TYPE_PD));
        beans.add(new TypeBean("补货单",false,Constant.TYPE_BH));
        beans.add(new TypeBean("调拨单",false,Constant.TYPE_DB));
        beans.add(new TypeBean("报损单",false,Constant.TYPE_BS));
        beans.add(new TypeBean("报溢单",false,Constant.TYPE_BY));



        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new DeleteAdapter(R.layout.item_delete, beans);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));

    }

    /**
     *初始化spinner
     */
    private void initSpinner() {
        ArrayAdapter adapter = new ArrayAdapter(mContext, android.R.layout.simple_list_item_1, statuses);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                sta = statuses[i];
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

    }
}
